package chat;

import io.netty.buffer.ByteBuf;
import io.netty.channel.*;

/**
 * @Author 刘牌
 * @Date 2020/3/16 0016 16:01
 * @Version 1.0
 */
@ChannelHandler.Sharable
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf in = (ByteBuf) msg;
        int len = in.readableBytes();
        byte[] bytes = new byte[len];
        in.getBytes(0,bytes);
        System.out.println("收到客户端发来的消息如下 "+new String(bytes,"UTF-8"));

        ChannelFuture channelFuture = ctx.writeAndFlush(msg);
        channelFuture.addListener((ChannelFutureListener) -> {
            ((ByteBuf) msg).refCnt();
            //System.out.println("写回后 "+((ByteBuf) msg).refCnt());
        });
    }
}
